package com.example.calculation;


import com.example.entity.Chef;
import com.example.entity.Equip;
import com.example.entity.OfficialGameData;
import com.example.entity.Recipe;

import java.util.Arrays;
import java.util.List;

public class PlayChef implements Cloneable{

    //id 根据厨师和菜谱生成
    private long id;

    private Chef chef;

    public PlayRecipe[] recipes = null;

    private Equip equip;

    public SkillEffect effect;

    public SkillEffect effectCache;

    public Chef getChef() {
        return chef;
    }

    public void setChef(Chef chef) {
        this.chef = chef.clone();
    }


    public PlayRecipe[] getRecipes() {
        return recipes;
    }

    public void setRecipes(PlayRecipe[] recipes) {
        this.recipes = recipes;
    }

    public long getId() {
        return id;
    }

    public Equip getEquip() {
        return equip;
    }

    public void setEquip(Equip equip) {
        this.equip = equip;
    }

    public void setId(long id) {
        this.id = id;
    }

    public SkillEffect getEffect() {
        return effect;
    }

    public void setEffect(SkillEffect effect) {
        this.effect = effect;
    }


    public PlayChef() {

    }

    public SkillEffect getEffectCache() {
        return effectCache;
    }

    public void setEffectCache(SkillEffect effectCache) {
        this.effectCache = effectCache;
    }

    @Override
    public PlayChef clone() {
        PlayChef old = null;
        try {
            old = (PlayChef) super.clone();

        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        return old;
    }


    public PlayChef(Chef chef) {
        this.chef = chef;
    }


    @Override
    public String toString() {

        if (equip != null) {
            return "{" +
                    "\"chef\": \"" + chef.getName() + "\"" +
                    ", \"recipes\":" + Arrays.toString(recipes) +
                    ", \"equip\": \"" + equip.getName() + "\"" +
                    '}';
        }
        return "{" +
                "\"chef\": \"" + chef.getName() + "\"" +
                ", \"recipes\":" + Arrays.toString(recipes) +
                ", \"equip\": \"\"" +
                '}';
    }

    public static class builder{
        OfficialGameData officialGameData;
        String chefname;
        String equipname;
        String[] recipenames;
        int[] cookingQuantity;

        public builder setPlay(String chefname, String equipname, String... params) {
            this.chefname = chefname;
            this.equipname = equipname;
            this.recipenames = params;
            return this;
        }

        public builder setQuantity(int... params) {
            this.cookingQuantity = params;
            return this;
        }

        public builder setGameData(OfficialGameData officialGameData) {
            this.officialGameData = officialGameData;
            return this;
        }

        public PlayChef build() {
            PlayChef playChef = new PlayChef();
            List<Recipe> recipes = officialGameData.getRecipes();
            List<Chef> chefs = officialGameData.getChefs();
            List<Equip> equips = officialGameData.getEquips();
            // ArrayList<PlayRecipe> playRecipes = new ArrayList<PlayRecipe>(recipenames.length);
            PlayRecipe[] playRecipes = new PlayRecipe[recipenames.length];

            for (int i = 0, index = 0; i < recipenames.length; i++) {
                String recipename = recipenames[i];
                for (Recipe recipe : recipes) {
                    if (recipe.getName().equals(recipename)) {
                        PlayRecipe playRecipe = new PlayRecipe(recipe, cookingQuantity[i]);
                        // playRecipes.add(playRecipe);
                        playRecipes[index++] = playRecipe;
                        break;
                    }
                }
            }
            playChef.setRecipes(playRecipes);
            for (Chef chef : chefs) {
                if (chef.getName().equals(chefname)) {
                    playChef.setChef(chef);
                    break;
                }
            }
            for (Equip equip : equips) {
                if (equip.getName().equals(equipname)) {
                    playChef.setEquip(equip);
                    break;
                }
            }
            return playChef;
        }
    }


}
